package com.ruoyi.web.core.redpacket;

import com.ruoyi.web.core.utils.SendResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.net.ssl.SSLContext;
import java.io.InputStream;
import java.security.KeyStore;
import java.util.Iterator;
import java.util.SortedMap;
import java.util.TreeMap;

@Slf4j
public class TransferToRedpacket extends Transfer {

    private static final String TRANSFERS_PAY = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";

    public String wxSendWallet(String merchantId, String secret, String appId, String openId, String orderId, String actName, int amount) {

        String data = null;
        try {

            String nonceStr = RandomStringUtils.randomAlphabetic(32);

            SortedMap parameters = new TreeMap();

            parameters.put("nonce_str", nonceStr);

            parameters.put("mch_id", merchantId);

            parameters.put("mch_billno", orderId);

            parameters.put("wxappid", appId);

            parameters.put("send_name", actName);

            parameters.put("re_openid", openId);

            parameters.put("total_amount", amount);

            parameters.put("total_num", 1);

            parameters.put("wishing", "恭喜你获得红包");

            parameters.put("client_ip", "139.224.103.17");

            parameters.put("act_name", actName);

            parameters.put("remark", actName);

            parameters.put("scene_id", "PRODUCT_2");

            parameters.put("sign", generateSignature(parameters, secret));

            data = mapToXml(parameters);

            log.info("xml:{}", data);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return data;
    }

    @Override
    protected String getUrl() {
        return TRANSFERS_PAY;
    }

    @Override
    protected String getCert() {
        return "cert/big.p12";
    }


}
